# 方法一
class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        dit = defaultdict(int)
        for i in range(len(nums)):
            dit[nums[i]] += 1
        
        ans = 0
        for x in nums:
            if k - x in dit:
                if dit[x] < 1:
                    continue
                dit[x] -= 1
                if dit[k - x] < 1:
                    continue
                dit[k - x] -= 1
                ans += 1
        return ans


# 方法一代码优化
class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        dit = defaultdict(int)
        ans = 0
        for x in nums:
            if dit[k - x]:
                dit[k - x] -= 1
                ans += 1
            else:
                dit[x] += 1
        return ans